//
//  ViewController.m
//  GLWaterFlowView
//
//  Created by CoderGL on 15/11/27.
//  Copyright © 2015年 GL. All rights reserved.
//

#import "ViewController.h"
#import "MJExtension.h"
#import "MJRefresh.h"

#import "GLWaterFlowView.h"
#import "GLShopCell.h"
#import "GLShop.h"


@interface ViewController () <GLWaterFlowViewDataSoruce, GLWaterFlowViewDelegate>
@property (nonatomic, strong) NSMutableArray *shops;
@property (nonatomic, weak) GLWaterFlowView *waterFlowView;

@end

@implementation ViewController

- (NSMutableArray *)shops {
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取数据
    NSArray *array = [GLShop objectArrayWithFilename:@"1.plist"];
    [self.shops addObjectsFromArray:array];
    
    // 创建瀑布流控件
    GLWaterFlowView *waterFlowView = [[GLWaterFlowView alloc] init];
    waterFlowView.frame = self.view.bounds;
    waterFlowView.backgroundColor = [UIColor yellowColor];
    waterFlowView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    
    waterFlowView.dataSource = self;
    waterFlowView.delegate = self;
    [self.view addSubview:waterFlowView];
    self.waterFlowView = waterFlowView;
    
    
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    //    NSLog(@"屏幕旋转完毕");
    [self.waterFlowView reloadData];
}


- (NSUInteger)numberOfCellsInWaterFlowView:(GLWaterFlowView *)waterFlowView {
    return self.shops.count;
}

- (NSUInteger)numberOfColumnInWaterFlowView:(GLWaterFlowView *)waterFlowView {
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        return 3;
    } else {
        return 5;
    }
}

- (GLWaterFlowViewCell *)waterFlowView:(GLWaterFlowView *)waterFlowView cellAtIndex:(NSUInteger)index {
    GLShopCell *cell = [GLShopCell cellWithWaterFlowView:waterFlowView];
    cell.shop = self.shops[index];
    return cell;
}

- (CGFloat)waterFlowView:(GLWaterFlowView *)waterFlowView heightAtIndex:(NSUInteger)index {
    GLShop *shop = self.shops[index];
    
    // 根据cell的宽度 和 图片的宽高比 算出 cell的高度
    return waterFlowView.cellWidth * shop.h / shop.w;
}

- (void)waterFlowView:(GLWaterFlowView *)waterFlowView didSelectAtIndex:(NSUInteger)index {
    
    NSLog(@"%d", index);
    
}


@end
